package com.smcdd.cdb.dbo.cdb_dbo.ui.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 作者：Administrator on 2016/9/22 16:35
 * 邮箱：zhanghuaiha@gmail.com
 */

public class AssetsAnalysis {

    private static BufferedReader br;


    public static void writeToFile(String fileName, String result)
            throws IOException {
        //String filePath = "D:\\" + fileName+".txt";
        File file = new File(fileName);
        if (!file.isFile()) {
            file.createNewFile();
            DataOutputStream out = new DataOutputStream(new FileOutputStream(
                    file));
            out.writeBytes(result);
        }
    }


    /**
     * 从assets中读取地区数据,保存为stringbuffer类型
     */
    public static String getData(InputStream in) {

        ByteArrayOutputStream out = null;
        try{
        out =   new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return out.toString();
    }


    public static String toString(InputStream input) {
        String string = null;
        try {
            string = new String(toByteArray(input));
        } catch (IOException e) {
           // LogUtil.e(TAG, "Exception " + e + "occur when IOUtils.toString");
        }
        return string;
    }


    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream(4 * 1024);
        byte[] buffer = new byte[1024];
        int n = 0;

        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }

}
